package com.woniu.microcommunityserver.book.mapper;

import com.woniu.microcommunityserver.book.entity.BookInformationInfoEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface BookInformationInfoMapper {
    /**
     * 查询所有图书
     * @return
     */
    @Select("select id,book_code,book_name,book_author,book_concern,book_remark,book_warehousing,book_state " +
            "from book_information_info")
    @ResultMap(value = "bookInformationInfoMap")
    List<BookInformationInfoEntity> findAll();

    /**
     * 查询指定图书
     * @return
     */
    @Select({"<script> " +
            "select id,book_code,book_name,book_author,book_concern,book_remark,book_warehousing," +
            "book_state from book_information_info"+
            "<where>" +
            "<if test='bookName!= null'>" +
            "and book_name like concat('%', #{bookName}, '%')"+
            "</if>"+
            "<if test='bookAuthor!= null'>" +
            "and book_author like concat('%', #{bookAuthor}, '%')"+
            "</if>"+
            "<if test='bookState!= null'>" +
            "and book_state = #{bookState}"+
            "</if>"+
            "</where>"+
            "</script>"})
    @ResultMap(value = "bookInformationInfoMap")
    List<BookInformationInfoEntity> findAny(Map<String,Object> map);

    /**
     * 新增图书
     * @param bookInformationInfoEntity
     * @return
     */
    @Insert("insert into book_information_info(book_code,book_name,book_author,book_concern," +
            "book_number,book_remark,book_warehousing,book_state) values(#{b.bookCode},#{b.bookName}," +
            "#{b.bookAuthor},#{b.bookConcern},#{b.bookNumber}," +
            "#{b.bookRemark},#{b.bookWarehousing},#{b.bookState}) ")
    boolean insertBook(@Param("b") BookInformationInfoEntity bookInformationInfoEntity);

    /**
     * 根据id删除图书
     * @param id
     * @return
     */
    @Delete("delete from book_information_info where id=#{id}")
    boolean deleteBook(Integer id);

    /**
     * 借出图书
     * @return
     */
    @Update("update book_information_info set book_state=1 where id=#{id}")
    boolean loanBook(Integer id);

    /**
     * 归还图书
     * @return
     */
    @Update("update book_information_info set book_state=0 where id=#{id}")
    boolean backBook(Integer id);

    /**
     * 修改图书信息
     * @param bookInformationInfoEntity
     * @return
     */
    @Update({"<script>",
            "update book_information_info",
            "<set>",
            "<if test='bookCode!= null'>",
            "book_code = #{bookCode} ,",
            "</if>",
            "<if test='bookName != null'>",
            "book_name = #{bookName} ,",
            "</if>",
            "<if test='bookAuthor != null'>",
            "book_author = #{bookAuthor} ,",
            "</if>",
            "<if test='bookConcern != null'>",
            "book_concern = #{bookConcern} ,",
            "</if>",
            "<if test='bookRemark != null'>",
            "book_remark = #{bookRemark} ,",
            "</if>",
            "<if test='bookWarehousing != null'>",
            "book_warehousing = #{bookWarehousing} ,",
            "</if>",
            "</set>",
            "where id = #{id}",
            "</script>"})
    boolean updateBook(BookInformationInfoEntity bookInformationInfoEntity);


    @Select("select id,book_code,book_name from book_information_info where id=#{id}")
    @ResultMap(value = "bookInformationInfoMap")
    BookInformationInfoEntity findOne(Integer id);
}
